<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page contentType="text/html; charset=big5" language="java" import="util.*, java.sql.*,beans.*, java.text.*" errorPage="" %>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Catalogue Page</title>
        <link href="astyles.css" rel="stylesheet" type="text/css" />
        
    </head>
    <body>
        <div class="absolute_title">Captain Hook's Store</div>
        <div class="absolute_title2">
            <h5>
                <%
                String username = (String)session.getAttribute("username");
                boolean flag = false;
                if(username==null) {
                    out.println("Please <a href='login.jsp'>Login.</a>");
                }else {
                %>
                <h5>Hi!<%= username%><br/>
                <a href="logout.jsp"> Click here to logout</a></h5>
                <% }
                %>
            </h5>
        </div>
        <div class="wrap">
            <div class="canvas">
                <div class="left_column">
                    <div class="header_left">
                        <div class="header_leftrowone">&nbsp;</div>
                        <div class="header_leftrowtwo"><img src="../image/cpt_hook_banner1.jpg" width="123" height="87" /></div>
                        <div class="header_leftrowthree"></div>
                    </div>
                    <div class="leftnav">
                        <ul>
                            <li></li>
                        </ul>
                    </div>
                    <div>
                        <SCRIPT src="displayMyFlash1.js"></SCRIPT>
                    </div>
                </div>
                <div class="right_column">
                    <div class="header_right">
                        <div class="header_rightrowone">&nbsp;</div>
                        <div class="header_rightrowtwo">&nbsp;</div>
                        <div class="header_rightrowthree">
                            <ul class="topmenu">
                                <li><a title="Home" 
                                       href="index.jsp">Home</a> | </li>
                                <li><a title="Information" 
                                       href="companybackground.html">Information</a> | </li>
                                <li><a title="Forum" 
                                       href="http://www.voy.com/206926/">Discussion Forum</a></li>
                            </ul>
                        </div>
                    </div>
                    <div class="content_wrapper">
                        <div class="content_left">
                            <div>
                                <form action='SearchServlet' method='get'>
                                    Enter keyword to search:
                                    <input type='text' name='keyword'/>
                                    <input name='submit' type='submit' value='Search' />
                                    <a href="AdvanceSearch.jsp">Advanced search</a>
                                </form>
                            </div>
                            <h2>Catalogue Page</h2>
                            <table width="100%" height="100%" border="1">
                                <tr bgcolor="white">
                                    <td width="49"><b>Item ID:</b></td>
                                    <td width="40"><b>Name</b></td>
                                    <td width="54"><b>Company</b></td>
                                    <td width="40"><b>Scale</b></td>
                                    <td width="46"><b>Price</b></td>
                                    <td width="160"><b>Image (Click to view its details)</b></td>
                                    <td width="49"><b>Quantity</b></td>
                                    <td width="47"><b>Buy</b></td>
                                </tr>
                                <%
                                
                                
                                final String sPage = (String)request.getParameter("page");
                                int page1 = 0; //<====
                                final int recordPerPage = 5;
                                final int indexPerPage =10;
                                final int count = ModelBean.getTotalRecordCount(); //<=======
                                final int indexCount = (int)Math.floor(count / (double)recordPerPage);
                                if(page1 > indexCount) {
                                    page1 = 0;
                                }
                                if(util.Utilities.isValid(sPage)) {
                                    try {
                                        page1 = Integer.parseInt(sPage);
                                    } catch(Exception e) {
                                        
                                    }
                                }
                                final int k=page1;
                                
                                
                                %>
                                <%
                                
                                try {
                                    String color = null;
                                    ModelBean[] models = ModelBean.getAllModel();
                                    for(int z=recordPerPage*page1; z<(page1+1)*recordPerPage && z<models.length; z++ ) {
                                        
                                        int quantity = models[z].getQuantity();
                                        
                                        if (flag) {
                                            flag = false;
                                            color = "#9DC4EA";
                                        } else {
                                            flag = true;
                                            color = "#B2ECB3";
                                        }
                                        
                                        String itemId = models[z].getItemID();
                                        String name = models[z].getName();
                                        String type = models[z].getType();
                                        String company = models[z].getCompany();
                                        String scale = models[z].getScale();
                                        String price = models[z].getPrice();
                                        String description = models[z].getDescription();
                                        String imageURL = models[z].getImageURL();
                                        DecimalFormat df = new DecimalFormat("#.00");
                                
                                
                                %>
                                <form action='SavecartServlet' method='get'>
                                    <tr bgcolor="<%= color%>">
                                        <td><%=itemId%>
                                            <input type='hidden' name='itemID' value='<%=itemId%>' />
                                        </td>
                                        <td><%=name%>
                                            <input type='hidden' name='name' value='<%=name%>' />
                                        </td>
                                        
                                        <input type='hidden' name='type' value='<%=type%>' />
                                        
                                        <td><%=company%>
                                            <input type='hidden' name='company' value='<%=company%>' />
                                        </td>
                                        <td><%=scale%>
                                            <input type='hidden' name='scale' value='<%=scale%>' />
                                        </td>
                                        <td> $<%=df.format(Double.parseDouble(price))%>
                                            <input type='hidden' name='price' value='<%=price%>' />
                                        </td>
                                        <td align="center" valign="middle"><a href='ItemDetail.jsp?imageURL=<%=imageURL%>'><img src='../image/<%=imageURL%>' width="160" height="200" border="0"/></a>
                                        <input type='hidden' name='imageURL' value='<%=imageURL%>' /></td>
                                        <td><%if(quantity == 0) {%>
                                            <font color="red">Out of Stock</font>
                                            <%}else{%>
                                            <select name="quantity">
                                                <option value="1">1</option>
                                                <option value="2">2</option>
                                                <option value="3">3</option>
                                                <option value="4">4</option>
                                                <option value="5">5</option>
                                                <option value="6">6</option>
                                                <option value="7">7</option>
                                                <option value="8">8</option>
                                                <option value="9">9</option>
                                                <option value="10">10</option>
                                            </select>
                                            <%}%>
                                        </td>
                                        <% if(quantity == 0) { %>
                                        <td valign='middle'><input name="submit" type='submit' value='Buy' disabled/>                      </td>
                                        <% } else { %>
                                        <td valign='middle'><input name="submit" type='submit' value='Buy'/>                      </td>
                                        <%}%>
                                    </tr>
                                </form>
                                <%
                                
                                    }
                                } catch(Exception e) {
                                    e.printStackTrace();
                                }
                                %>
                                <%!                                
                                void printIndex(int k, int i, JspWriter out1) {
                                    try {
                                        if (i==k) {
                                            out1.println("<b><a href='Catalogue.jsp?page="+i+"'>"+(i+1)+"</a></b>");
                                        }else {
                                            out1.println("<a href='Catalogue.jsp?page="+i+"'>"+(i+1)+"</a>");
                                        }
                                    }catch(Exception e) {
                                        
                                    }
                                }
                                
                                %>
                            </table>
                            <p>&nbsp;</p>
                            <div class="post_btm">
                                <form method='get' action='Catalogue.jsp'>
                                    <center>
                                        <%
                                        int nextValue = 5;
                                        int remain = count-(indexCount+page1);
                                        if (remain < indexPerPage) {
                                            nextValue = remain % indexPerPage;
                                        }
                                        if(k < 5) {
                                            for(int i=0; i<=indexCount&&i<indexPerPage; i++) {
                                                printIndex(k, i, out);
                                            }
                                        }else if(indexCount < indexPerPage) {
                                            for(int i=0; i<=indexCount; i++) {
                                                printIndex(k, i, out);
                                            }
                                        }else if(k >= indexCount-indexCount%indexPerPage -1) {
                                            int start = indexCount - indexPerPage;
                                            for(int y=0; y <indexPerPage; y++) {
                                                printIndex(k, y+start, out);
                                            }
                                        }else {
                                            for (int i=0; i<5; i++) {
                                                printIndex(k, page1-5+i, out);
                                            }
                                            for (int i=0; i< 5&& page1+i<indexCount; i++) {
                                                printIndex(k, page1+i, out);
                                            }
                                        }
                                        %>
                                        <input type='hidden' name='page' value='<%= k+1%>' />
                                        <input name="submit" type = "submit" value='Next Page'/>
                                    </center>
                                </form>
                            </div>
                        </div>
                    </div>
                    <div class="footer">
                        <ul class="basemenu">
                            <li></li>
                        </ul>
                        Designed by Captain Hook inc, all rights reserved. <br />
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>
